package com.chenggong.log.dialog

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.*
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.chenggong.log.LogRecord
import com.chenggong.log.R
import com.chenggong.log.adapter.ViewLogAdapter
import com.chenggong.log.databinding.DialogViewLogBinding

class ViewLogDialog: DialogFragment() {
    private var toast: Toast? = null
    private lateinit var mDataBinding: DialogViewLogBinding


    private val viewLogAdapter by lazy {
        ViewLogAdapter().apply {
            itemClickListener = { veiw, data, position ->
                showToast(data.msg)
            }
            tagClickListener = { veiw, data, position ->
                //showToast(data.tag)
                filter(data.tag)
            }
            levelClickListener = { veiw, data, position ->
                //showToast(data.tag)
                filter2(data.level.tag)
            }
        }
    }

    private fun showToast(msg: String) {
        toast?.cancel()
        toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT)
        toast?.show()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置dialog样式
        setStyle(STYLE_NO_TITLE, R.style.BottomDialogStyle)
    }

    override fun onStart() {
        super.onStart()
        dialog?.window!!.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        dialog?.window!!.setGravity(Gravity.TOP)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mDataBinding = DialogViewLogBinding.inflate(inflater,container,false)

        dialog?.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //设置背景为透明
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)

        initView()
        return mDataBinding.root
    }

    private fun initView() {
        mDataBinding.apply {
            mDataBinding.recyclerView.layoutManager = LinearLayoutManager(context)
            mDataBinding.recyclerView.adapter = viewLogAdapter
            mDataBinding.tvClose.setOnClickListener {
                dismiss()
            }
            mDataBinding.tvCopy.setOnClickListener {
                copy(viewLogAdapter.getAllContent())
            }
            mDataBinding.tvPure.setOnClickListener {
                viewLogAdapter.changeDisplay()
            }
        }
    }

    fun updateData(logRecord: LogRecord) {
        viewLogAdapter.updateData(logRecord)
    }

    private fun copy(str: String) {
        if (!TextUtils.isEmpty(str)) {
            val cm = context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val data = ClipData.newPlainText("simple text", str)
            cm.setPrimaryClip(data)

            showToast("已复制")
        }else{
            showToast("复制内容不能为空")
        }
    }
}